package com.luo.controller;

import com.alibaba.fastjson.JSON;
import com.luo.model.GoodByTwoClassIdRespone;
import com.luo.model.Goods;
import com.luo.model.Result;
import com.luo.model.ResultObj;
import com.luo.service.GoodService;
import com.luo.util.ListTypes;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 功能：搜索商品根据商品名称
 * 作者：Luo。
 * 日期：2024/3/11 21:14
 */
@WebServlet("/queryGoodsByNameLike")
public class QueryGoodsByNameLikeController extends HttpServlet {

	GoodService goodService = new GoodService();

	@Override
	protected void service(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
		ResultObj resultObj = new ResultObj();
		String name = req.getParameter("goodName");
		if (name == null || "".equals(name)) {
			resultObj.setCode("500");
			resultObj.setMsg("商品名称为空");
			String jsonString = JSON.toJSONString(resultObj);
			resp.getWriter().write(jsonString);
			resp.getWriter().close();
		}
		List<Goods> goods = goodService.queryGoodsByNameLike(name);
		resultObj.setCode("200");
		resultObj.setMsg("查询成功");
		GoodByTwoClassIdRespone goodByTwoClassIdRespone = new GoodByTwoClassIdRespone();
		goodByTwoClassIdRespone.setItems(goods);
		resultObj.setResult(goodByTwoClassIdRespone);
		String jsonString = JSON.toJSONString(resultObj);
		resp.getWriter().write(jsonString);
		resp.getWriter().close();
	}
}
